// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

import java.util.List;

import junit.framework.AssertionFailedError;

public class ElementGeneratorTest {

    @Test
    public void shouldCreateCodeForSomeWellKnownElements() {
        List<CompilationUnit> compilationUnits = new ElementGenerator().generate();

        assertIncludes(compilationUnits, interfaceWithName("Html"));
        assertIncludes(compilationUnits, interfaceWithName("Div"));
        assertIncludes(compilationUnits, interfaceWithName("Table"));

        assertIncludes(compilationUnits, interfaceWithName("HtmlContent"));
        assertIncludes(compilationUnits, interfaceWithName("DivContent"));
        assertIncludes(compilationUnits, interfaceWithName("TableContent"));
    }

    private InterfaceWithNamePredicate interfaceWithName(final String name) {
        return new InterfaceWithNamePredicate(name);
    }

    private <S> void assertIncludes(List<S> interfaces, Predicate<S> predicate) {
        for (S item : interfaces) {
            if (predicate.test(item)) {
                return;
            }
        }
        throw new AssertionFailedError("Did not contain item as expected");
    }

    static interface Predicate<S> {
        boolean test(S object);
    }

    private static class InterfaceWithNamePredicate implements Predicate<CompilationUnit> {
        private final String name;

        public InterfaceWithNamePredicate(String name) {
            this.name = name;
        }

        public boolean test(CompilationUnit generatedInterface) {
            return generatedInterface.getClassName().equals(name);
        }

        public MethodExpectations containingMethod(String methodName) {
            return new MethodExpectations(this, methodName);
        }
    }

    private static class MethodExpectations implements Predicate<CompilationUnit> {
        private final Predicate<CompilationUnit> parent;
        private final String methodName;

        public MethodExpectations(Predicate<CompilationUnit> parent, String methodName) {
            this.parent = parent;
            this.methodName = methodName;
        }

        public boolean test(CompilationUnit object) {
            if (!parent.test(object)) {
                return false;
            }
            for (Member member : object.getMembers()) {
                if (member instanceof AbstractMethod
                        && ((AbstractMethod) member).getMethodName().equals(methodName)) {
                    return true;
                }
            }
            return false;
        }

        public MethodReturnTypeExpectations thatReturns(String returnType) {
            return new MethodReturnTypeExpectations(this, methodName, returnType);
        }
    }

    private static class MethodReturnTypeExpectations implements Predicate<GeneratedInterface> {
        private MethodExpectations parent;
        private String methodName;
        private String returnType;

        public MethodReturnTypeExpectations(MethodExpectations parent, String methodName, String returnType) {
            this.parent = parent;
            this.methodName = methodName;
            this.returnType = returnType;
        }

        public boolean test(GeneratedInterface object) {
            if (!parent.test(object)) {
                return false;
            }
            for (Member member : object.getMembers()) {
                if (member instanceof AbstractMethod
                        && ((AbstractMethod) member).getMethodName().equals(methodName)
                        && ((AbstractMethod) member).getReturnType().equals(returnType)) {
                    return true;
                }
            }
            return false;
        }
    }
}
